package com.lovegu.other;

import org.junit.Test;

import java.util.Optional;

/**
 * @author 老顾
 * @title: OptionalTest
 * @projectName java-restudy
 * @email: 1437594522@qq.com
 * @date 2022/9/26 17:27
 */
public class OptionalTest {
    @Test
    public void test1(){
        Boy boy = new Boy("张三");
        Optional<Girl> opt1 = Optional.ofNullable(boy.getGrilFriend());
        opt1.ifPresent(System.out::println);
    }
    @Test
    public void Test2(){
        Boy boy = new Boy("张三");
        Optional<Girl> opt = Optional.ofNullable(boy.getGrilFriend());
        Girl girl = opt.orElse(new Girl("嫦娥"));
        System.out.println("他的女朋友是"+girl.getName());
    }
    @Test
    public void Test3(){
        Optional<Employee> opt = Optional.of(new Employee("张三", 8888));
        Optional<Employee> employee = opt.filter(e -> e.getSalary() > 10000);
        System.out.println(employee);
    }
}
class Boy{

    public Boy(String name) {
    }

    public Girl getGrilFriend() {
        Girl girl = new Girl("成为女朋友");
        return girl;
    }
}
class Girl{
    private String name;
    public Girl(String name){

    }

    public String getName() {
        return "test";
    }

    public void setName(String name) {
        this.name = name;
    }
}
class Employee{

    public Employee(String name, int i) {
    }

    public Integer getSalary() {
        return 111111;
    }
}